package com.metal.gear.certification.config;

import com.metal.gear.certification.entity.po.SysUser;
import com.metal.gear.certification.service.ISysUserService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Objects;

@Component
public class MetalGearUserDetailsService implements UserDetailsService {

    ISysUserService userService;

    public MetalGearUserDetailsService(ISysUserService userService) {
        this.userService = userService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser userInDb = userService.getByName(username);
        if (Objects.nonNull(userInDb)){
            return new User(userInDb.getUsername(),userInDb.getPassword(),new ArrayList<>());
        }
        throw new UsernameNotFoundException(String.format( "用户【%s】不存在",username));
    }
}
